package inet.app.vod.activity.film;

import inet.app.vod.activity.R;
import inet.app.vod.activity.common.BaseActivity;
import inet.app.vod.adapter.VideoSummaryAdapter;
import inet.app.vod.config.AppConstants;
import inet.app.vod.net.http.VolleyClient;
import inet.app.vod.net.http.VolleyListener;
import inet.app.vod.object.CategoryModel;
import inet.app.vod.object.MovieModel;
import inet.app.vod.object.ResultJsonModel;
import inet.app.vod.parser.Parse;
import inet.app.vod.util.MyUtil;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.inet.app.framework.intentutil.LaunchIntent;
import com.inet.app.framework.util.Log;
import com.inet.app.framework.widget.MyToast;

public class IPhimCategoryActivity extends BaseActivity {

	private GridView mGridView;
	private PullToRefreshGridView mPullRefreshGridView;

	private CategoryModel mCategory;
	private VideoSummaryAdapter mVideoSummaryAdapter;
	private List<MovieModel> mMovieModels;
	private String mHomeId;
	private int mPage = AppConstants.START_PAGE;
	private int mNumColumns;

	OnRefreshListener2<GridView> onRefresh = new OnRefreshListener2<GridView>() {

		@Override
		public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
			requestListItem();
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
			requestListItem();
		}

	};

	private void initModels() {

		mNumColumns = getResources().getInteger(R.integer.num_columns);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			mCategory = (CategoryModel) bundle.getSerializable("category");
			mHomeId = bundle.getString("home_id");
		}
		setTitle(mCategory.getName());
	}

	private void initViews() {
		mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.gridview);
		mPullRefreshGridView.setPullLabel(getString(R.string.keo_de_tai_them));
		mPullRefreshGridView.setRefreshingLabel(getString(R.string.dang_tai));
		mPullRefreshGridView
				.setReleaseLabel(getString(R.string.tha_de_bat_dau_tai));
		mPullRefreshGridView.setOnRefreshListener(onRefresh);
		mGridView = mPullRefreshGridView.getRefreshableView();
		mGridView.setAdapter(mVideoSummaryAdapter);
		mGridView.setOnScrollListener(mScrollingListener);
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long resId) {
				MovieModel movie = mMovieModels.get(position);
				Bundle bundle = new Bundle();
				bundle.putSerializable("movie", movie);
				bundle.putString("home_id", mHomeId);
				LaunchIntent.start(IPhimCategoryActivity.this,
						IPhimDetailActivity.class, bundle);
			}

		});
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_iphim_category);
		initUIBase();
		initModels();
		initViews();
		requestListItem();
	}

	private void requestListItem() {
		VolleyClient.requestItems(mHomeId, mCategory.getRemoteId(),
				AppConstants.getPageSize(getApplicationContext()), mPage,
				new VolleyListener(this) {

					@Override
					public void onSuccess(String response) {
						Log.d("success request" + response);
						ResultJsonModel jResult = Parse
								.parseMovieListHot(response);
						if (jResult == null) {
							// error parser
							MyUtil.showDialog("Lỗi server!",
									IPhimCategoryActivity.this);
						} else if (jResult.hasError()) {
							// error register
							String error = jResult.getError();
							MyUtil.showDialog(error, IPhimCategoryActivity.this);
						} else {
							// Save categories to database
							List<MovieModel> newMovies = (List<MovieModel>) jResult
									.getResult();
							if (newMovies == null || newMovies.size() == 0) {
								MyToast.show(IPhimCategoryActivity.this,
										"Đã tải hết dữ liệu");
								mPullRefreshGridView.onRefreshComplete();
								return;
							}
							if (mPage == 1) {
								mMovieModels = newMovies;
							} else {
								mMovieModels.addAll(newMovies);
							}
							showListMovies();
							mPage++;
						}
					}

					@Override
					public void onStart() {
						Log.d("start request");
						showLoading(true);
					}

					@Override
					public void onFinish() {
						Log.d("start request");
						showLoading(false);
					}

					@Override
					public void onError(VolleyError error) {
						MyUtil.showDialog("Không thể tải danh sách phim trong "
								+ mCategory.getName(),
								IPhimCategoryActivity.this);
					}
				});
	}

	private void showListMovies() {
		mPullRefreshGridView.onRefreshComplete();
		if (mPage == 1) {
			mVideoSummaryAdapter = new VideoSummaryAdapter(
					getApplicationContext(), 0, mMovieModels, false);
			mGridView.setAdapter(mVideoSummaryAdapter);
			return;
		} else {
			mVideoSummaryAdapter.notifyDataSetChanged();
		}
		int newFirstPos = (mPage - 1) * AppConstants.getPageSize(this);
		if (android.os.Build.VERSION.SDK_INT > 10) {
			mGridView.smoothScrollToPositionFromTop(newFirstPos + mNumColumns,
					0);
		} else {
			mGridView.smoothScrollToPosition(newFirstPos + mNumColumns);
		}
	}

	@Override
	public void setTagMenu() {
		if (mHomeId.equals(AppConstants.ID_IPHIM)) {
			mTagMenu = AppConstants.MENU_IPHIM;
		} else {
			mTagMenu = AppConstants.MENU_ICLIP;
		}
	}

}